import Cookies from 'js-cookie'
import {getAddMsgList} from "@/api/message";
import {getFriendList} from "@/api/user";
import {getConversationList} from "@/api/conversation";

const getDefaultState = ()=>{
  return {
    // 保存chat侧边栏活跃的状态
    sidebarActive:Cookies.get('sidebarActive'),
    friendActive:'', // 朋友页面活跃组件
    currentFriendInfo:'', // 当前查看的朋友信息
    friendList:[], // 朋友列表
    requestMessageList:[], // 请求添加好友消息列表
    conversationList:[], // 会话列表
    currentConversation:'',
    conversationMessagesList:[],// 会话消息列表
    uploadProgress:{show:false,total:0,loaded:0} // 上传进度
  }
}

const state = getDefaultState()

const  mutations ={
  RESET_STATE: (state) => {
    // Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象
    Object.assign(state, getDefaultState())
  },
  SET_SIDEBAR_ACTIVE:(state,active)=>{
    state.sidebarActive = active
    Cookies.set('sidebarActive',active)
  },
  SET_FRIEND_ACTIVE:(state,active)=>{
    state.friendActive = active
  },
  SET_CURRENT_FRIEND_INFO:(state,info)=>{
    state.currentFriendInfo = info
  },
  SET_REQUEST_MESSAGE_LIST:(state,messageList)=>{
    state.requestMessageList = messageList
  },
  ADD_REQUEST_MESSAGE_LIST:(state,message)=>{
    state.requestMessageList.push(message)
  },
  REMOVE_REQUEST_MESSAGE:(state,index)=>{
    state.requestMessageList.splice(index,1)
  },
  SET_FRIEND_LIST:(state,friendList)=>{
    state.friendList = friendList
  },
  ADD_FRIEND_LIST:(state,friend)=>{
    state.friendList.push(friend)
  },
  SET_CONVERSATION_LIST:(state,conversationList)=>{
    state.conversationList = conversationList
  },
  ADD_CONVERSATION_LIST:(state,conversation)=>{
    state.conversationList.unshift(conversation)
  },
  SET_CURRENT_CONVERSATION:(state,user)=>{
    state.currentConversation = user
  },
  REMOVE_CONVERSATION:(state,index)=>{
    state.conversationList.splice(index,1)
  },
  ADD_CONVERSATION_MESSAGES_LIST:(state,conversationMessages)=>{
    state.conversationMessagesList.push(conversationMessages)
  },
}

const actions = {
  // 获取请求添加好友消息列表
  getRequestMessageList({commit}){
    new Promise((resolve,reject)=>{
      getAddMsgList().then(res=>{
        let messageList = res.content.messageList;
        commit('SET_REQUEST_MESSAGE_LIST', messageList);
        console.log("请求ADD-FRIEND消息成功！")
        resolve(messageList)
      }).catch(err=>{
        reject(err)
      })
    })
  },
  // 获取朋友列表
  getFriendList({commit}){
    new Promise((resolve,reject)=>{
      getFriendList().then(res=>{
        let friendList = res.content.friendList;
        commit('SET_FRIEND_LIST',friendList)
        resolve(friendList)
      }).catch(err=>reject(err))
    })
  },
  // 获取会话列表
  getConversationList({commit}){
    new Promise((resolve,reject)=>{
      getConversationList().then(res=>{
        commit('SET_CONVERSATION_LIST',res.content.conversationList)
        console.log("请求会话列表完成！")
        resolve(res)
      }).catch(err=>reject(err))
    })
  }
}


export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
